﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace LcfContols
{
    /// <summary>
    /// 扩展List增加各个事件
    /// </summary>
    public class LEventList<T> : IList<T>
    {
        #region 事件
        /// <summary>
        /// 移除事件
        /// 第一个参数：对象实例
        /// 第二个参数：移除的索引
        /// 第三个参数：移除的实例
        /// </summary>
        public event Action<object, int, T> RemoveEvent;

        /// <summary>
        /// 添加事件
        /// 第一个参数：对象实例
        /// 第二个参数：添加后对象索引
        /// </summary>
        public event Action<object, int> AddEvent;

        /// <summary>
        /// 插入事件
        /// 第一个参数：对象实例
        /// 第二个参数：插入的索引值
        /// </summary>
        public event Action<object, int> InsertEvent;

        /// <summary>
        /// 清空事件
        /// 第一个参数：对象实例
        /// </summary>
        public event Action<object> ClearEvent;
        #endregion

        #region 变量
        /// <summary>
        /// 实际实例
        /// </summary>
        private List<T> owner = new List<T>();
        #endregion

        public T this[int index]
        {
            get
            {
                return owner[index];
            }

            set
            {
                owner[index] = value;
            }
        }

        public int Count
        {
            get
            {
                return owner.Count;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public void Add(T item)
        {
            owner.Add(item);
            if (AddEvent != null)
                AddEvent(this, owner.IndexOf(item));
        }

        public void Clear()
        {
            owner.Clear();
            if (ClearEvent != null)
                ClearEvent(this);
        }

        public bool Contains(T item)
        {
            return owner.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            owner.CopyTo(array, arrayIndex);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return owner.GetEnumerator();
        }

        public int IndexOf(T item)
        {
            return owner.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            owner.Insert(index, item);
            if (InsertEvent != null)
                InsertEvent(this, index);
        }

        public bool Remove(T item)
        {
            int index = owner.IndexOf(item);
            if(owner.Remove(item))
            {
                if(RemoveEvent != null){
                    RemoveEvent(this, index, item);
                }
                return true;
            }
            return false;
        }

        public void RemoveAt(int index)
        {
            T item = owner[index];
            RemoveAt(index);
            if (RemoveEvent != null)
                RemoveEvent(this, index, item);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return owner.GetEnumerator();
        }
    }
    
}
